#include "stdafx.h"

#define INITGUID

#include "protocol.h"
#include <map>

bool operator <(const GUID &guid1, const GUID &guid2)
{
	return 0 > memcmp(&guid1, &guid2, sizeof(GUID));
}

class CProtocols: public std::map<GUID, IProtocol*>
{
public:
	CProtocols()
	{

	}
	
	~CProtocols()
	{
		iterator iProtocol = begin();
		for(;iProtocol != end();++iProtocol)
		{
			(iProtocol->second)->Release();
		} // for
	}
};

CProtocols g_Protocols;

STDMETHODIMP RegisterProtocol(LPGUID lpProtocolID, IProtocol* lpNewProtocol)
{
	HRESULT hResult = E_INVALIDARG;
	if(NULL != lpNewProtocol && NULL != lpProtocolID)
	{
		CProtocols::iterator iProtocol = g_Protocols.find(*lpProtocolID);
		if(iProtocol == g_Protocols.end()) 
		{
			lpNewProtocol->AddRef();
			g_Protocols[*lpProtocolID] = lpNewProtocol;
		} // if
	} // if

	return hResult;
}

STDMETHODIMP GetSession(LPGUID lpProtocolID, ISession ** lppSesion)
{
	HRESULT hResult = E_INVALIDARG;
	if(NULL != lpProtocolID && NULL != lppSesion)
	{
		hResult = MAPI_E_NOT_FOUND;
		CProtocols::iterator iProtocol = g_Protocols.find(*lpProtocolID);
		if(iProtocol != g_Protocols.end()) 
		{
			hResult = (iProtocol->second)->CreateSession(lppSesion);
		} // if
	} // if
	return hResult;
}